package com.wngbms.concurrent.future;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

@Slf4j
public class FutureTaskDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> ft2 = new FutureTask<>(new Task2());
        FutureTask<String> ft1 = new FutureTask<>(new Task1(ft2));

        new Thread(ft1).start();
        new Thread(ft2).start();

        log.info(ft1.get());
    }

    public static class Task1 implements Callable<String> {

        FutureTask<String> ft2;

        public Task1(FutureTask<String> ft2) {
            this.ft2 = ft2;
        }

        @Override
        public String call() throws Exception {
            log.info("洗水壶");
            TimeUnit.SECONDS.sleep(1);
            log.info("烧开水");
            TimeUnit.SECONDS.sleep(15);

            String tf = ft2.get();
            log.info("拿到茶叶:{}", tf);

            return "上茶:" + tf;
        }
    }

    public static class Task2 implements Callable<String> {

        @Override
        public String call() throws Exception {
            log.info("洗茶壶");
            TimeUnit.SECONDS.sleep(1);
            log.info("洗茶杯");
            TimeUnit.SECONDS.sleep(1);
            log.info("拿茶叶");
            TimeUnit.SECONDS.sleep(1);

            return "龙井";
        }
    }

}
